#include <sourcemod>
#include <sdktools>
#include <l4d_stocks>
#include <l4d_weapon_stocks>
#include <sdkhooks>
#include <left4downtown>

#include "macros.sp"
#include "helpers.sp"

#define DENY_SOUND "ui/menu_invalid.wav"

#define PLUGIN_VERSION "1.9"

new Handle:cvar_timetokeep_1 = INVALID_HANDLE;
new Handle:cvar_timetokeep_2 = INVALID_HANDLE;
new Handle:cvar_botblocktime_1 = INVALID_HANDLE;
new Handle:cvar_botblocktime_2 = INVALID_HANDLE;

new bool:surv_out = false;

public Plugin:myinfo = 
{
	name = "Round Start Bot Stop + No rush start",
	author = "EHG",
	description = "Round Start Bot Stop + no rush start",
	version = PLUGIN_VERSION,
	url = ""
}


new bool:first_round = true;
new bool:timer_no_ready = true;
new time_left = 0;


public OnPluginStart()
{
	CreateConVar("l4d2_RSBS_version", PLUGIN_VERSION, "RSBS version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD)

	cvar_timetokeep_1 = CreateConVar("l4d2_norushstart_time_first", "60.0", "How many time before open the safe door room on first round");
	cvar_timetokeep_2 = CreateConVar("l4d2_norushstart_time_second", "40.0", "How many time before open the safe door room on second round");
	cvar_botblocktime_1 = CreateConVar("l4d2_RSBS_botblock_first", "50.0", "How many time a bot is blocked in safe room on first round", CVAR_FLAGS);
	cvar_botblocktime_2 = CreateConVar("l4d2_RSBS_botblock_second", "20.0", "How many time a bot is blocked in safe room on second round", CVAR_FLAGS);
	
	HookEvent("round_start", Event_round_start);
	HookEvent("versus_round_start", event_versus_round_start);

}

public OnEntityCreated(entity, const String:classname[])
{
	if(strcmp(classname, "prop_door_rotating_checkpoint") == 0 || strcmp(classname, "checkpoint_exit") == 0 || strcmp(classname, "checkpoint_entrance") == 0) // a safe room door
	{
		SDKHook(entity, SDKHook_Use, OnEntityUse);
	}
}
public OnClientPutInServer(client)
{
	SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}

public OnClientDisconnect(client)
{
	SDKUnhook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}

public Action:OnTakeDamage(entity, &inflictor, &attacker, &Float:damage, &damageType, &weapon, const Float:damageForce[3], const Float:damagePosition[3])
{
	if(!surv_out)
	{
		if(IsValidSurvivor(entity))
		{
			damage = 0.0;
			return Plugin_Changed;
		}
	}

	return Plugin_Continue;
}


public OnMapStart()
{
	PrecacheSound(DENY_SOUND);
	SetConVarInt(FindConVar("sb_stop"), 1);
}

public OnMapEnd()
{
	first_round = true; //called at each map change

}


public Action:Event_round_start(Handle:event, const String:name[], bool:dontBroadcast)
{

	SetConVarInt(FindConVar("sb_stop"), 1);
	timer_no_ready = true;
	surv_out = false;

	if(first_round) //1st round
	{
		first_round = false;
		time_left = GetConVarInt(cvar_timetokeep_1);
		CreateTimer(GetConVarFloat(cvar_botblocktime_1), unlockbot, _, TIMER_FLAG_NO_MAPCHANGE);
		CreateTimer(1.0, timer_tick, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
		
	}
	else //2nd round
	{
		time_left = GetConVarInt(cvar_timetokeep_2);
		CreateTimer(GetConVarFloat(cvar_botblocktime_2), unlockbot, _, TIMER_FLAG_NO_MAPCHANGE);

		CreateTimer(1.0, timer_tick, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
	}
}

public Action:event_versus_round_start(Handle:event, const String:name[], bool:dontBroadcast)
{
	SetConVarInt(FindConVar("sb_stop"), 0);
	surv_out = true;
}

/*change a cvar to make bot active*/
public Action:unlockbot(Handle:timer, any:junk)
{
	SetConVarInt(FindConVar("sb_stop"), 0);
}

public Action:timer_tick(Handle:timer, any:junk)
{
	time_left--;
	
	if(time_left == 0)
	{
		timer_no_ready = false;
		KillTimer(timer);
	}
}

public Action:OnEntityUse(entity, client)
{

	if(timer_no_ready)
	{
		if(!IsValidEntity(entity))
			return Plugin_Continue;	

		if(!IsValidSurvivor(client))
			return Plugin_Continue;	

		AcceptEntityInput(entity, "Close");
		AcceptEntityInput(entity, "forceclosed");
		AcceptEntityInput(entity, "Lock");
		CreateTimer(0.2, close_door, entity, TIMER_FLAG_NO_MAPCHANGE);

		PrintToChat(client, "\x05Round start in %d seconds\x01", time_left);
		EmitSoundToAll(DENY_SOUND, entity);

		return Plugin_Handled;
	}
	else
	{
		/*unlock the safe door*/
		if(GetEntProp(entity, Prop_Data, "m_bLocked") == 1)
			AcceptEntityInput(entity, "Unlock");
	}

	return Plugin_Continue;
}

public Action:close_door(Handle:timer, any:entity)
{
	AcceptEntityInput(entity, "Close");
	AcceptEntityInput(entity, "forceclosed");
	AcceptEntityInput(entity, "Lock");
}